﻿
//封装 callBack=提交后执行的js. 一般为对返回信息的处理. 默认执行服务端js.
function ajaxSubmit(selected, callBack) {
    var fm = $(selected);

    if (fm[0].tagName != 'FORM') {
        fm = $(fm[0].form);
    }

    var isOk = fm.valid();

    if (isOk) {
        if (callBack == undefined) {
            callBack = function (rt) { try { eval(rt); } catch (e) { $.jBox.tip('真抱歉,提交失败.', 'error'); } };
        }

        $.jBox.tip("正在提交，请稍等…", 'loading');

        $.ajax({
            type: "POST",
            url: fm.attr('action') == '' ? window.location.href : fm.attr('action'),
            data: fm.serializeArray(),
            success: callBack,
            error: function (rt) { if (rt.status == 0) return; tip('服务端返回' + rt.status + '错误.', 'error'); }
        });

    }
    //$.post(fm.attr('action') == '' ? window.location.href : fm.attr('action'), fm.serializeArray(), callBack); 

    return false;
}

/* Ajax命令, 由服务端决定ajax的后续执行过程 */
function ajaxCmd(url, parms, tipTxt) {
    if (tipTxt == undefined) {
        tipTxt = "正在提交,请稍等.";
    }
    if (tipTxt != '') {
        tip(tipTxt, "loading");
    }

    if (typeof (parms) == 'number') {
        parms = parms.toString();
    }

    var callBack = function (rt) { try { eval(rt); } catch (e) { tip('无效的服务端指令.', 'error'); } };

    $.ajax({
        type: "POST",
        url: url,
        data: parms,
        success: callBack,
        error: function (rt) { if (rt.status == 0) return; tip('服务端返回' + rt.status + '错误.', 'error'); }
    });

    return false;
}

/* Ajax请求, return 请求返回的内容 同步方法 */
function ajaxQuery(url, parms) {
    var rt = $.ajax({
        url: url,
        data: parms,
        async: false
    });
    
    if (rt.status != 200) {
        tip('服务端返回' + rt.status + '错误.', 'error');
        throw '服务端返回' + rt.status + '错误.';
    }
    if (rt.responseText == "404") {
        $.jBox.tip("资源不存在！");
        return "";
    }
    if (rt.responseText == "nologin") {
        $.jBox.tip("请登录后再试！");
        return "";
    }
    if (rt.responseText == "noauth") {
        $.jBox.tip("无权限执行此项操作！");
        return "";
    }
    return rt.responseText;
}

function ajaxPost(url, parms) {
    var rt = $.ajax({
        type: "POST",
        url: url,
        data: parms,
        async: false
    });

    if (rt.status != 200) {
        tip('服务端返回' + rt.status + '错误.', 'error');
        throw '服务端返回' + rt.status + '错误.';
    }
    if (rt.responseText == "404") {
        $.jBox.tip("资源不存在！");
        return "";
    }
    if (rt.responseText == "nologin") {
        $.jBox.tip("请登录后再试！");
        return "";
    }
    if (rt.responseText == "noauth") {
        $.jBox.tip("无权限执行此项操作！");
        return "";
    }
    return rt.responseText;
}

function excFilter(txt) {
    switch (txt) {
        case "404":
            $.jBox.tip("资源不存在！");
        default:
    }
}

// 小提示一个
function tip(txt, type, p1, p2) {
    //设置了 callBack 则出现屏蔽层
    var time, callBack;

    switch (typeof (p1)) {
        case "number":
            time = p1;
        case "function":
            callBack = p1;
    }

    switch (typeof (p2)) {
        case "number":
            time = p2;
        case "function":
            callBack = p2;
    }

    if (time == undefined) {
        time = txt.length * 200;
    }

    return $.jBox.tip(txt, type, { timeout: time, opacity: (callBack ? 0.1 : 0), closed: callBack });
}